package com.joyxy.spider.mapscript;

import java.util.List;
import com.joyxy.spider.mapscript.operation.*;

/**
 * 地图脚本使用示例
 */
public class MapScriptExample {
    
    public static void main(String[] args) {
        // 示例脚本内容（天狼脚本格式）
        String scriptContent = "[天狼]$\n" +
                "[]$\n" +
                "7295%13%42%130%133%1|13/42/130/133/29+6|6+1|13/42/130/133/42+3|1/42/132/10#\n" +
                "7295%55%77%153%155%1|55/77/153/155/57+3|0/57/154/10#\n" +
                "7295%19%49%147%149%1|2/49/147/149/34+6|6+1|2/49/147/149/21+3|0/21/148/15#\n" +
                "7295%1%23%139%141%1|2/23/139/141/11+3|1/11/140/5#\n" +
                "$[]\n";
        
        // 解析脚本
        MapScript script = MapScript.parseFromString(scriptContent);
        
        System.out.println("=== 脚本解析结果 ===");
        System.out.println("提示信息: " + script.getHint());
        System.out.println("保留位1: '" + script.getReserved1() + "'");
        System.out.println("保留位2: '" + script.getReserved2() + "'");
        System.out.println("总层数: " + script.getLayers().size());
        
        // 显示所有层信息
        System.out.println("\n=== 地图层信息 ===");
        for (int i = 0; i < script.getLayers().size(); i++) {
            ScriptLayer layer = script.getLayers().get(i);
            System.out.println("层 " + (i + 1) + ": " + layer);
            
            // 显示该层的所有操作
            System.out.println("  操作命令:");
            for (ScriptOperation operation : layer.getOperations()) {
                System.out.println("    " + operation);
                printOperationDetails(operation);
                
                // 演示新的操作类转换
                BaseOperation baseOp = operation.toBaseOperation();
                System.out.println("      → 转换为: " + baseOp.getDetailInfo());
                if (baseOp.isValid()) {
                    System.out.println("      → 状态: 有效");
                } else {
                    System.out.println("      → 状态: 无效");
                }
            }
            System.out.println();
        }
        
        // 分析脚本结构
        System.out.println("=== 脚本结构分析 ===");
        analyzeScriptStructure(script);
        
        // 测试根据坐标查找适用的操作（基于实际脚本坐标）
        System.out.println("\n=== 坐标查找测试 ===");
        testPositionLookup(script, 29, 131); // 第一层范围内
        testPositionLookup(script, 60, 154); // 第二层范围内
        testPositionLookup(script, 11, 140); // 第四层范围内
        testPositionLookup(script, 100, 200); // 超出范围
        
        // 演示具体操作类的使用
        System.out.println("\n=== 操作类演示 ===");
        demonstrateOperationClasses();
        
        // 演示脚本执行
        System.out.println("\n=== 脚本执行演示 ===");
        demonstrateScriptExecution(script);
    }
    
    /**
     * 打印操作详细信息
     */
    private static void printOperationDetails(ScriptOperation operation) {
        int type = operation.getOperationType();
        int[] params = operation.getParameters();
        
        switch (type) {
            case OperType.HORIZONTAL_MOVE:
                if (params != null && params.length >= 5) {
                    System.out.println("      → 水平移动: 区域(" + params[0] + "," + params[1] + "," + params[2] + "," + params[3] + ") 目标X=" + params[4]);
                }
                break;
            case OperType.VERTICAL_MOVE:
                if (params != null && params.length >= 6) {
                    System.out.println("      → 垂直移动: 区域(" + params[0] + "," + params[1] + "," + params[2] + "," + params[3] + ") 目标Y=" + params[4] + " 模式=" + params[5]);
                }
                break;
            case OperType.JUMP:
                if (params != null && params.length >= 4) {
                    System.out.println("      → 跳跃: 方向=" + params[0] + " 坐标(" + params[1] + "," + params[2] + ") 持续时间=" + params[3]);
                }
                break;
            case OperType.MAP_CHANGE:
                if (params != null && params.length >= 3) {
                    System.out.println("      → 地图转换: 坐标(" + params[0] + "," + params[1] + ") 按键=" + params[2]);
                }
                break;
            case OperType.LURE_MONSTER:
                if (params != null && params.length >= 7) {
                    System.out.println("      → 吹怪: 起点=" + params[0] + " 终点=" + params[1] + " 引怪时间=" + params[2] + 
                                     " 吹怪点=" + params[3] + " 方向=" + params[4] + " 休息时间=" + params[5] + " 坐下=" + params[6]);
                }
                break;
            case OperType.ATTACK:
                if (params != null && params.length >= 5) {
                    System.out.println("      → 攻击: 区域(" + params[0] + "," + params[1] + "," + params[2] + "," + params[3] + ") 时间=" + params[4] + "秒");
                }
                break;
            default:
                System.out.println("      → 未知操作类型: " + type);
                break;
        }
    }
    
    /**
     * 分析脚本结构
     */
    private static void analyzeScriptStructure(MapScript script) {
        System.out.println("脚本格式: [提示]$[保留位]$[地图及命令信息]$[保留位]");
        System.out.println("提示词: " + script.getHint());
        System.out.println("第一保留位: " + (script.getReserved1().isEmpty() ? "(空)" : script.getReserved1()));
        System.out.println("第二保留位: " + (script.getReserved2().isEmpty() ? "(空)" : script.getReserved2()));
        
        // 分析地图数据
        if (script.getLayers().size() > 0) {
            System.out.println("地图ID: " + script.getLayers().get(0).getMapId() + " (所有层使用相同地图)");
            
            // 计算整体坐标范围
            int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
            int minY = Integer.MAX_VALUE, maxY = Integer.MIN_VALUE;
            
            for (ScriptLayer layer : script.getLayers()) {
                minX = Math.min(minX, layer.getxMin());
                maxX = Math.max(maxX, layer.getxMax());
                minY = Math.min(minY, layer.getyMin());
                maxY = Math.max(maxY, layer.getyMax());
            }
            
            System.out.println("整体坐标范围: (" + minX + "," + minY + ") 到 (" + maxX + "," + maxY + ")");
            System.out.println("活动区域大小: " + (maxX - minX + 1) + " x " + (maxY - minY + 1));
        }
    }
    
    /**
     * 测试根据坐标查找操作
     */
    private static void testPositionLookup(MapScript script, int x, int y) {
        System.out.println("\n坐标 (" + x + ", " + y + ") 的操作:");
        
        ScriptLayer layer = script.findLayerByPosition(x, y);
        if (layer != null) {
            System.out.println("  所在层: " + layer);
            
            List<ScriptOperation> operations = script.findOperationsByPosition(x, y);
            if (operations.isEmpty()) {
                System.out.println("  无适用操作");
            } else {
                System.out.println("  适用操作:");
                for (ScriptOperation operation : operations) {
                    System.out.println("    " + operation);
                }
            }
        } else {
            System.out.println("  不在任何层范围内");
        }
    }
    
    /**
     * 演示各种操作类的使用
     */
    private static void demonstrateOperationClasses() {
        System.out.println("创建并测试各种操作类:");
        
        // 演示水平移动
        HorizontalMoveOperation hMove = new HorizontalMoveOperation(10, 50, 20, 30, 35);
        System.out.println("1. " + hMove.getDetailInfo());
        System.out.println("   在坐标(25,25)执行: " + hMove.isInExecutionArea(25, 25));
        hMove.execute();
        
        // 演示跳跃
        JumpOperation jump = new JumpOperation(1, 100, 50, 10);
        System.out.println("\n2. " + jump.getDetailInfo());
        System.out.println("   跳跃类型: " + (jump.isRightJump() ? "右跳" : "其他"));
        jump.execute();
        
        // 演示攻击
        AttackOperation attack = new AttackOperation(300);
        System.out.println("\n3. " + attack.getDetailInfo());
        System.out.println("   长时间攻击: " + attack.isLongAttack());
        System.out.println("   短时间攻击: " + attack.isShortAttack());
        attack.execute();
        
        // 演示地图转换
        MapChangeOperation mapChange = new MapChangeOperation(200, 100, 1);
        System.out.println("\n4. " + mapChange.getDetailInfo());
        System.out.println("   使用钥匙: " + mapChange.isUsingKey());
        mapChange.execute();
        
        // 演示吹怪
        LureMonsterOperation lure = new LureMonsterOperation(0, 100, 30, 50, 1, 10, 1);
        System.out.println("\n5. " + lure.getDetailInfo());
        System.out.println("   移动距离: " + lure.getMoveDistance());
        System.out.println("   长时间引怪: " + lure.isLongTimeLure());
        lure.execute();
        
        // 演示未知操作
        UnknownOperation unknown = new UnknownOperation(99, new int[]{1, 2, 3});
        System.out.println("\n6. " + unknown.getDetailInfo());
        unknown.execute();
    }
    
    /**
     * 演示脚本执行功能
     */
    private static void demonstrateScriptExecution(MapScript script) {
        System.out.println("演示不同的脚本执行方式:");
        
        // 1. 执行整个脚本
        System.out.println("\n1. 执行整个脚本:");
        script.execute();
        
        // 2. 执行指定坐标的操作
        System.out.println("\n2. 执行指定坐标的操作:");
        script.executeAtPosition(29, 131); // 第一层范围内
        
        // 3. 执行指定地图的所有层
        System.out.println("\n3. 执行指定地图的所有层:");
        script.executeForMap(7295);
        
        // 4. 执行单个层
        System.out.println("\n4. 执行单个层:");
        if (!script.getLayers().isEmpty()) {
            ScriptLayer firstLayer = script.getLayers().get(0);
            firstLayer.execute();
        }
    }
    
    /**
     * 创建一个更复杂的示例脚本用于测试
     */
    public static String createComplexScriptExample() {
        return "复杂地图脚本示例$预留位置1$" +
                // 第一层：移动和攻击
                "100%0%200%0%100%1|0/200/0/100/150+6|50/150/20/80/60#" +
                // 第二层：跳跃和地图转换
                "100%200%400%0%100%3|1/300/50/10+4|350/75/0#" +
                // 第三层：吹怪
                "100%0%500%100%200%5|0/500/30/250/1/10/1#" +
                "$预留位置2";
    }
} 